﻿using System.Web;
using System.Web.Optimization;
using System;
using Clrizr.Web.App_Start.Configuration;
using BundleTransformer.Core.Transformers;
using BundleTransformer.Core.Orderers;

namespace Clrizr.Web
{
    public class BundleConfig
    {
        public const string JQuery = "~/bundles/jquery";
        public const string JQueryUnobtrusive = "~/bundles/jqueryunobtrusive";
        public const string JQueryValidate = "~/bundles/jqueryvalidate";
        public const string JQueryValidateUnobtrusive = "~/bundles/jqueryvalidateunobtrusive";

        public const string JQueryUi = "~/bundles/jqueryui";

        public const string Modernizr = "~/bundles/modernizr";
        public const string Knockout = "~/bundles/knockout";

        public const string JQueryRotate = "~/bundles/jqueryrotate";

        public const string JQueryUiCss = "~/styles/jqueryui/css";
        public const string CoreCss = "~/styles/corecss";

        public static void RegisterBundles(BundleCollection bundles)
        {
            BundleConfig.AddCdnBundles(bundles);

            bundles.Add(new ScriptBundle(JQueryRotate).Include("~/Scripts/jQueryRotate.2.2.js"));

            BundleConfig.AddStyleBundles(bundles);
        }

        private static void AddStyleBundles(BundleCollection bundles)
        {
            var bundleVersions = CdnBundleVersionConfig.GetConfig();

            Bundle corecss = new Bundle(CoreCss);
            corecss.Transforms.Add(new CssTransformer());
            corecss.Transforms.Add(new CssMinify());
            corecss.Orderer = new NullOrderer();
            corecss.Include("~/styles/site.less");
            bundles.Add(corecss);

            StyleBundle jqueryuicss = new StyleBundle(JQueryUiCss);
            jqueryuicss.Transforms.Add(new CssTransformer());
            jqueryuicss.Transforms.Add(new CssMinify());
            jqueryuicss.Orderer = new NullOrderer();
            jqueryuicss.Include(String.Format("~/styles/jqueryui/jquery-ui-{0}.custom.css", bundleVersions.jQueryUiCss));

            bundles.Add(jqueryuicss);
        }

        private static void AddCdnBundles(BundleCollection bundles)
        {
            bundles.UseCdn = true;

            var bundleVersions = CdnBundleVersionConfig.GetConfig();

            Bundle jquery = new Bundle(JQuery).Include("~/Scripts/Cdn/jquery-1.8.2.js");
            jquery.CdnPath = String.Format("http://ajax.aspnetcdn.com/ajax/jQuery/jquery-{0}.min.js", bundleVersions.jQuery);
            bundles.Add(jquery);

            Bundle jqueryunobtrusive = new Bundle(JQueryUnobtrusive).Include("~/Scripts/Cdn/jquery.unobtrusive-ajax.js");
            jqueryunobtrusive.CdnPath = String.Format("http://ajax.aspnetcdn.com/ajax/mvc/{0}/jquery.unobtrusive-ajax.min.js", bundleVersions.jQueryUnobtrusive);
            bundles.Add(jqueryunobtrusive);

            Bundle jqueryvalidate = new Bundle(JQueryValidate).Include("~/Scripts/Cdn/jquery.validate.js");
            jqueryvalidate.CdnPath = String.Format("http://ajax.aspnetcdn.com/ajax/jquery.validate/{0}/jquery.validate.min.js", bundleVersions.jQueryValidate);
            bundles.Add(jqueryvalidate);

            Bundle jqueryvalidateunobtrusive = new Bundle(JQueryValidateUnobtrusive).Include("~/Scripts/Cdn/jquery.validate.unobtrusive.js");
            jqueryvalidateunobtrusive.CdnPath = String.Format("http://ajax.aspnetcdn.com/ajax/mvc/{0}/jquery.validate.unobtrusive.min.js", bundleVersions.jQueryValidateUnobtrusive);
            bundles.Add(jqueryvalidateunobtrusive);

            Bundle jqueryui = new Bundle(JQueryUi).Include("~/Scripts/Cdn/jquery-ui.js");
            jqueryui.CdnPath = String.Format("http://ajax.aspnetcdn.com/ajax/jquery.ui/{0}/jquery-ui.min.js", bundleVersions.jQueryUi);
            bundles.Add(jqueryui);

            Bundle modernizr = new Bundle(Modernizr).Include("~/Scripts/Cdn/modernizr-2.0.6-development-only.js");
            modernizr.CdnPath = String.Format("http://ajax.aspnetcdn.com/ajax/modernizr/modernizr-{0}-development-only.js", bundleVersions.Modernizr);
            bundles.Add(modernizr);

            Bundle knockout = new Bundle(Knockout).Include("~/Scripts/Cdn/knockout-2.1.0.debug.js");
            knockout.CdnPath = String.Format("http://ajax.aspnetcdn.com/ajax/knockout/knockout-{0}.js", bundleVersions.Knockout);
            bundles.Add(knockout);
        }
    }
}